package com.gxa.logistics.controller;

import com.gxa.logistics.common.base.RespResult;
import com.gxa.logistics.domain.dto.*;
import com.gxa.logistics.domain.vo.*;
import com.gxa.logistics.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "统计报表模块")
@RestController
@RequestMapping("/Statistics")
public class StatisticsController {

    @Autowired
    private CarRecordService carRecordService;
    @Autowired
    private ElectricityRecordService electricityRecordService;
    @Autowired
    private WireRecordService wireRecordService;
    @Autowired
    private NewRecordService newRecordService;
    @Autowired
    private CableRecordService cableRecordService;
    @Autowired
    private MaterialRecordService materialRecordService;
    @Autowired
    private PersonRecordService personRecordService;

    @ApiOperation("出车统计")
    @PostMapping("/SelectCarRecord")
    public RespResult selectCarRecord(String year){
            List<CarRecordVO> carList = carRecordService.selectAllCar(year);
            int total = 0;//所有出过任务的车，已去重
            for(int i=0;i<carList.size();i++){
                int t1=carRecordService.selectCarMonthNum1(carList.get(i).getCarId());
                int t2=carRecordService.selectCarMonthNum2(carList.get(i).getCarId());
                int t3=carRecordService.selectCarMonthNum3(carList.get(i).getCarId());
                int t4=carRecordService.selectCarMonthNum4(carList.get(i).getCarId());
                int t5=carRecordService.selectCarMonthNum5(carList.get(i).getCarId());
                int t6=carRecordService.selectCarMonthNum6(carList.get(i).getCarId());
                int t7=carRecordService.selectCarMonthNum7(carList.get(i).getCarId());
                int t8=carRecordService.selectCarMonthNum8(carList.get(i).getCarId());
                int t9=carRecordService.selectCarMonthNum9(carList.get(i).getCarId());
                int t10=carRecordService.selectCarMonthNum10(carList.get(i).getCarId());
                int t11=carRecordService.selectCarMonthNum11(carList.get(i).getCarId());
                int t12=carRecordService.selectCarMonthNum12(carList.get(i).getCarId());
                carList.get(i).setCarTaskNum1(t1);
                carList.get(i).setCarTaskNum2(t2);
                carList.get(i).setCarTaskNum3(t3);
                carList.get(i).setCarTaskNum4(t4);
                carList.get(i).setCarTaskNum5(t5);
                carList.get(i).setCarTaskNum6(t6);
                carList.get(i).setCarTaskNum7(t7);
                carList.get(i).setCarTaskNum8(t8);
                carList.get(i).setCarTaskNum9(t9);
                carList.get(i).setCarTaskNum10(t10);
                carList.get(i).setCarTaskNum11(t11);
                carList.get(i).setCarTaskNum12(t12);
                total=t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12;
                carList.get(i).setTotal(total);
           }
        return RespResult.success(carList);
    }

    @ApiOperation("变电统计")
    @PostMapping("/SelectElectricityRecord")
    public RespResult selectElectricityRecord(@RequestBody ElectricityRecordDTO electricityRecordDTO){
        List<ElectricityRecordVO> electricityRecordVOS = electricityRecordService.selectElectricityRecord(electricityRecordDTO);
        return RespResult.success(electricityRecordVOS);
    }

    @ApiOperation("导线统计")
    @PostMapping("/SelectWireRecord")
    public RespResult selectWireRecord(@RequestBody WireRecordDTO wireRecordDTO){
        List<WireRecordVO> wireRecordVOS = wireRecordService.selectWireRecord(wireRecordDTO);
        return RespResult.success(wireRecordVOS);
    }

    @ApiOperation("新品统计")
    @PostMapping("/SelectNewRecord")
    public RespResult selectNewRecord(@RequestBody NewRecordDTO newRecordDTO){
        List<NewRecordVO> newRecordVOS = newRecordService.selectNewRecord(newRecordDTO);
        return RespResult.success(newRecordVOS);
    }

    @ApiOperation("电缆统计")
    @PostMapping("/SelectCableRecord")
    public RespResult selectCableRecord(@RequestBody CableRecordDTO cableRecordDTO){
        List<CableRecordVO> cableRecordDTOS = cableRecordService.selectCableRecord(cableRecordDTO);
        return RespResult.success(cableRecordDTOS);
    }

    @ApiOperation("物料统计")
    @PostMapping("/SelectMaterialRecord")
    public RespResult selectMaterialRecord(@RequestBody MaterialRecordDTO materialRecordDTO){
        List<MaterialRecordVO> materialRecordVOS = materialRecordService.selectAllOpMaterial(materialRecordDTO);//所有进行过出入库操作的物料，已经去重
        for (int i =0;i<materialRecordVOS.size();i++){
            Double inNum =00.00;
            Double outNum =0.00;
            List<MaterialRecordVO> materialRecordVOList=materialRecordService.selectInNum(materialRecordVOS.get(i).getMaterialId());
            for (int j=0;j<materialRecordVOList.size();j++){
                inNum+= materialRecordVOList.get(j).getInNum();
            }
            List<MaterialRecordVO> materialRecordVOList1=materialRecordService.selectOutNum(materialRecordVOS.get(i).getMaterialId());
            for (int j=0;j<materialRecordVOList1.size();j++){
                outNum+=materialRecordVOList1.get(j).getOutNum();
            }
            materialRecordVOS.get(i).setInNum(inNum);
            materialRecordVOS.get(i).setOutNum(outNum);


        }
        return RespResult.success(materialRecordVOS);
    }


    @ApiOperation("人员任务统计")
    @PostMapping("/SelectPersonRecord")
    public RespResult selectPersonRecord( Integer year) {
        String years = year.toString();
        List<PersonRecordVO> personRecordVOList = personRecordService.selectAllPerson(years);//所有出过任务的车，已去重
        List<PersonRecordVO> personRecordVOS = new ArrayList<>();
        for (int i = 0; i < personRecordVOList.size(); i++) {
            personRecordVOS.add(personRecordVOList.get(i));
            int total = 0;
            PersonRecordVO personRecordVO = new PersonRecordVO();
            for (int j = 1; j <= 12; j++) {
                int monthTaskNum = 0;
                PersonRecordVO personRecordVO1 = new PersonRecordVO();
                monthTaskNum = personRecordService.selectPersonMonthNum(personRecordVOList.get(i).getPersonId(), j);
                personRecordVO1.setPersonTaskNum(monthTaskNum);
                personRecordVOS.add(personRecordVO1);
                total += monthTaskNum;
            }
            personRecordVO.setTotal(total);
            personRecordVOS.add(personRecordVO);
        }
        return RespResult.success(personRecordVOS);
    }



}
